//@author A0101655N
/**
 * This class handles both receiving and rendering output (from an object)
 */

package ui;

import logic.DisplayPackage;
import logic.InputDecoder;
import storage.Task;
import storage.TaskList;
import sharedUtils.Constants;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class CmdLineUI implements Runnable{
	
	protected static final String MESSAGE_WELCOME_TO_QUICK_TODO = "Welcome to QuickToDo!";
	protected static final String MESSAGE_NEXT_COMMAND = "Command: ";
	protected static final String CURRENT_DATE_TIME = "Current Date/Time: ";
	protected static final String COMMAND_HELP = "help";
	protected static final String COMMAND_EXIT = "exit";
	protected static final String COMMAND_QUIT = "quit";

	protected static final String MESSAGE_INVALID_COMMAND = "Command is invalid.";
	protected static final String MESSAGE_ADD_SUCCESSFUL = "You have added:";
	protected static final String MESSAGE_DELETE_SUCCESSFUL = "You have deleted:";
	protected static final String MESSAGE_SEARCH_SUCCESSFUL = "You have searched:";
	protected static final String MESSAGE_SEARCH_NOT_FOUND = "No match for:";
	protected static final String MESSAGE_EDIT_SUCCESSFUL = "You have edited:";
	protected static final String MESSAGE_UNDO_SUCCESSFUL = "Undo is completed.";
	protected static final String MESSAGE_REDO_SUCCESSFUL = "Redo is completed.";
	protected static final String MESSAGE_ADD_UNSUCCESSFUL = "Task addition is NOT completed.";
	protected static final String MESSAGE_DELETE_UNSUCCESSFUL = "Task deletion is NOT completed.";
	protected static final String MESSAGE_SEARCH_UNSUCCESSFUL = "Searching is NOT completed.";
	protected static final String MESSAGE_EDIT_UNSUCCESSFUL = "Editing is NOT completed.";
	protected static final String MESSAGE_UNDO_UNSUCCESSFUL = "Undo is NOT completed.";
	protected static final String MESSAGE_REDO_UNSUCCESSFUL = "Redo is NOT completed.";
	protected static final String MESSAGE_ERROR_NO_TITLE = "Please enter a title for your task.";
	
	protected static final String TABLE_LINE_PARAM_DELIMITER = "|";
	protected static final String TITLE_DESC_DELIMITER = " // ";
	protected static final String DATE_DELIMITER = "-";
	protected static final String TIME_DELIMITER = ":";
	protected static final String DATE_TIME_DELIMITER = " / ";
	protected static	   String TABLE_TOP_AND_BOTTOM           = "+-----------------------------------------------------------------------------------------------------------------------------+";
	protected static 	   String TABLE_DISPLAY_HEADER           = "|ID  |Priority|       Start        |        End         |    Title // Description                                             |";
	protected static 	   String TABLE_DISPLAY_ROW_DEMARCATION  = "+----+--------+--------------------+--------------------+---------------------------------------------------------------------+";
	protected static	   String TABLE_HELP_HEADER              = "|No| Command | Description                                                          | Example                                 |";
	protected static	   String TABLE_HELP_ROW_DEMARCATION     = "+--+---------+----------------------------------------------------------------------+-----------------------------------------+";
	protected static final String TABLE_ENTRY_FORMAT = TABLE_LINE_PARAM_DELIMITER + "%1$4d" + TABLE_LINE_PARAM_DELIMITER +  "%2$8s" + TABLE_LINE_PARAM_DELIMITER +  " %3$s " + TABLE_LINE_PARAM_DELIMITER +  " %4$s " + TABLE_LINE_PARAM_DELIMITER + " %5$s " + TABLE_LINE_PARAM_DELIMITER;
	protected static final String TABLE_ENTRY_OVERFLOW_FORMAT = TABLE_LINE_PARAM_DELIMITER + "    " + TABLE_LINE_PARAM_DELIMITER + "        "  + TABLE_LINE_PARAM_DELIMITER + "                    "  +   TABLE_LINE_PARAM_DELIMITER + "                    " +  TABLE_LINE_PARAM_DELIMITER + " %1$s " + TABLE_LINE_PARAM_DELIMITER;
	protected static int    TABLE_DATE_ALLOWANCE = 18;
	protected static String TABLE_DATE_PAD = "%-" + TABLE_DATE_ALLOWANCE + "s";
	protected static int    TABLE_DESCRIPTION_ALLOWANCE = 67;
	protected static String TABLE_DESCRIPTION_PAD = "%-" + TABLE_DESCRIPTION_ALLOWANCE + "s";
	protected static final String TABLE_PRIORITY_HIGH = "  HIGH  ";
	protected static final String TABLE_PRIORITY_LOW = "  LOW   ";
	protected static final String TABLE_EMPTY_DATE_FIELD = "        -         ";
	protected static final String TABLE_EMPTY_LOCATION_FIELD = "      -       ";
	protected static final String TABLE_EMPTY_TYPE_FIELD = "    -    ";
	protected final String LINE_BREAK = System.getProperty("line.separator");
	
    TaskList taskList = new TaskList();
    Scanner sc = new Scanner(System.in);
    
    /**
     * Default Constructor
     */
    public CmdLineUI() {
    	//do Nothing
    }
    
    /**
     * Constructor to set taskList to operate on
     * @param taskList. 
     */
    public CmdLineUI(TaskList taskList) {
    	this.taskList = taskList;
    }
    
	/**
	 * Run the UI
	 */
	public void runUI() {
		
		System.out.println(MESSAGE_WELCOME_TO_QUICK_TODO);
		System.out.print(MESSAGE_NEXT_COMMAND);

		String input;
		
		while (true) {
			input = sc.nextLine().toLowerCase(); // to ignore caseline.equals
						
			String output = processInput(input);
			
			System.out.println(output);
			System.out.println(CURRENT_DATE_TIME +  new Date());
			System.out.println();
			System.out.print(MESSAGE_NEXT_COMMAND);
		}
	}

	/**
	 * Process the user input, and pass it to InputDecoder (if the input is not exit/quit/help),
	 * then receive a DisplayPackage object, check the command code, and display relevant messages and output
	 * 
	 * @param input the string inputed by user
	 * 
	 * @return string of feedback messages or the table of all tasks if the user input is "display",
	 * or null if input is null
	 */
	private String processInput(String input) {
		
		if (input == null) {
			return null;
		} else {
			String outputLine = "";

			if (input.equals(COMMAND_EXIT) || input.equals(COMMAND_QUIT)) {
				System.exit(0);
			} else if (input.equals(COMMAND_HELP)) {
				displayQuickHelp();
			} else {
				InputDecoder id = new InputDecoder();
				DisplayPackage returnedObject;

				returnedObject = id.exec_command(input, taskList);

				switch (returnedObject.getCommandCode()) {
					case Constants.INVALID_CODE: {
						return MESSAGE_INVALID_COMMAND;
					}
					case Constants.ADD_CODE: {
						return MESSAGE_ADD_SUCCESSFUL + "\n" + displayNewTask(returnedObject);
					}
					case Constants.DELETE_CODE: {
						return MESSAGE_DELETE_SUCCESSFUL + "\n" + displayNewTask(returnedObject);
					}
					case Constants.SEARCH_CODE: {
						return MESSAGE_SEARCH_SUCCESSFUL + "\n" + displaySearchedTask(returnedObject);
					}
					case Constants.EDIT_CODE: {
						return MESSAGE_EDIT_SUCCESSFUL + "\n" + displayNewTask(returnedObject);
					}
					case Constants.UNDO_CODE: {
						return MESSAGE_UNDO_SUCCESSFUL;
					}
					case Constants.REDO_CODE: {
						return MESSAGE_REDO_SUCCESSFUL;
					}
					case Constants.DISPLAY_CODE: {
						displayAll(returnedObject);
						return outputLine;
					}
					case Constants.ADD_INVALID_CODE: {
						return MESSAGE_ADD_UNSUCCESSFUL;
					}
					case Constants.DELETE_INVALID_CODE: {
						return MESSAGE_DELETE_UNSUCCESSFUL;
					}
					case Constants.SEARCH_INVALID_CODE: {
						return MESSAGE_SEARCH_UNSUCCESSFUL;
					}
					case Constants.EDIT_INVALID_CODE: {
						return MESSAGE_EDIT_UNSUCCESSFUL;
					}
					case Constants.UNDO_INVALID_CODE: {
						return MESSAGE_UNDO_UNSUCCESSFUL;
					}
					case Constants.REDO_INVALID_CODE: {
						return MESSAGE_REDO_UNSUCCESSFUL;
					}
				}
			}
			return outputLine;
		}
	}

	/**
	 * Display quick help in a table format
	 */
	private void displayQuickHelp() {
		System.out.println("Quick Help:");
		System.out.println(TABLE_TOP_AND_BOTTOM);
		System.out.println(TABLE_HELP_HEADER);
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|1.| add     | to add a new task                                                    | add ! homework `desc calculus           |");
		System.out.println("|  |         | if followed by \"!\", this task is of high priority                    | pg 44 `date 131214 1300 `to             |");
		System.out.println("|  |         | followed by the title of the task (mandatory)                        | 131214 1400                             |");
		System.out.println("|  |         | followed by necessary fields such as:                                |                                         |");
		System.out.println("|  |         | 1. `date  if followed by 4-digit int, it's TIME in 24hr format: HHmm |>add a task titled \"homework\" (which is  |");
		System.out.println("|  |         |           if followed by 6-digit int, it's DATE: ddMMyy              | of high priority) with description      |");
		System.out.println("|  |         | 2. `to	 RANGE for TIME/DATE                                        | \"calculus pg 44\" and is planned to be   |");
		System.out.println("|  |         |           followed by another TIME/DATE field                        | done on 13 Dec 2014 from 1-2pm          |");
		System.out.println("|  |         | 3. `desc  DESCRIPTION - additional info of the task                  |                                         |");
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|2.| del     | to delete a specified task                                           | del homework                            |");
		System.out.println("|  |         | followed by some keywords from the task title/description            |>delete tasks with \"homework\" title/desc |");
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|3.| search  | to search some tasks that contain the specified keywords             | search calculus                         |");
		System.out.println("|  |         | followed by keywords of the task to be searched                      |>search tasks with \"calculus\" title/desc |");
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|4.| edit    | to edit a task                                                       | edit homework `date 1400 `to 1500       |");
		System.out.println("|  |         | followed by the title of the task                                    |>edit the time for tasks with title/desc |");
		System.out.println("|  |         | followed by the keyfield to be edited                                | \"homework\" from its previous time period|");
		System.out.println("|  |         | followed by the new info for the specified keyfield                  | (if any) to the new time period (2-3pm) |");
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|5.| undo    | to undo the previous action                                          | undo                                    |");
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|6.| redo    | to redo the previous action                                          | redo                                    |");
		System.out.println(TABLE_HELP_ROW_DEMARCATION);
		System.out.println("|7.| display | to display all tasks                                                 | display                                 |");
		System.out.println(TABLE_TOP_AND_BOTTOM);
	}
	
	/**
	 * Display the task details when "add" or "delete" or "edit" command is called
	 * @param pack. The DsiplayPackage object containing all the tasks
	 * @return the details of task being added/deleted/edited in a table
	 */
	public String displayNewTask(DisplayPackage pack) {
		StringBuffer screenTable = new StringBuffer();
		screenTable.append(TABLE_TOP_AND_BOTTOM + LINE_BREAK);
		screenTable.append(TABLE_DISPLAY_HEADER + LINE_BREAK);
		screenTable.append(TABLE_DISPLAY_ROW_DEMARCATION + LINE_BREAK);
		Task entry = pack.getTaskData();
		int index = entry.getId();
		String entryOutput = formatTask(entry, index);
		screenTable.append(entryOutput + LINE_BREAK);
		screenTable.append(TABLE_TOP_AND_BOTTOM + LINE_BREAK);										
		return screenTable.toString();
	}
	
	/**
	 * Display the details of searched tasks when "search" command is called
	 * @param pack. The DsiplayPackage object containing all the tasks
	 * @return the details of all tasks being searched in a table
	 */
	public String displaySearchedTask(DisplayPackage pack) {
		if(pack == null) {
			return null;
		}
		ArrayList<Task> allTask = pack.getList();
		if(allTask == null) {
			return null; //failed
		}
		StringBuffer screenTable = new StringBuffer();
		screenTable.append(TABLE_TOP_AND_BOTTOM + LINE_BREAK);
		screenTable.append(TABLE_DISPLAY_HEADER + LINE_BREAK);
		screenTable.append(TABLE_DISPLAY_ROW_DEMARCATION + LINE_BREAK);
		String entryOutput = "";
		
		for (int i = 0; i < allTask.size(); i++) {
			Task entry = allTask.get(i);
			int index = entry.getId();
			entryOutput = formatTask(entry, index);
			screenTable.append(entryOutput + LINE_BREAK);
			
			// if last entry, show the table bottom line instead
			if (i == allTask.size() - 1) {
				screenTable.append(TABLE_TOP_AND_BOTTOM + LINE_BREAK);
			} else {
				screenTable.append(TABLE_DISPLAY_ROW_DEMARCATION + LINE_BREAK);
			}
		}
												
		return screenTable.toString();
	}
	
	/**
	 * Display the total number of tasks and
	 * call printTaskList to display the details of all tasks when "display" command is called
	 * @param pack. The DsiplayPackage object containing all the tasks
	 */
	public void displayAll(DisplayPackage pack) {
		if(pack == null) {
			return; // failed: nothing to display
		}
		ArrayList<Task> allTask = pack.getList();
		if(allTask == null) {
			return; // failed: no task to display
		}
		System.out.println("You have " + allTask.size() + " task(s) in total.");
		System.out.print(printTaskList(allTask));	
	}
		
	/**
	 * Display all tasks to the user in a table
	 * @param allTask. An array list of tasks inside Task object
	 * @return a table of all tasks and their details
	 */
	public String printTaskList(ArrayList<Task> allTask) {
		assert(allTask != null);		// allTask has been checked and is not null
		
		StringBuffer screenTable = new StringBuffer();
		
		screenTable.append(TABLE_TOP_AND_BOTTOM + LINE_BREAK);
		screenTable.append(TABLE_DISPLAY_HEADER + LINE_BREAK);
		screenTable.append(TABLE_DISPLAY_ROW_DEMARCATION + LINE_BREAK);
		
		for (int i = 0; i < allTask.size(); i++) {
			Task entry = allTask.get(i);
			int index = entry.getId();
			String entryOutput = formatTask(entry, index);
			screenTable.append(entryOutput + LINE_BREAK);
			
			// if last entry, show the table bottom line instead
			if (i == allTask.size() - 1) {
				screenTable.append(TABLE_TOP_AND_BOTTOM + LINE_BREAK);
			} else {
				screenTable.append(TABLE_DISPLAY_ROW_DEMARCATION + LINE_BREAK);
			}
		}
		
		return screenTable.toString();			
	}
	
	/**
	 * Format the task details so that they can be displayed in a table
	 */
	private String formatTask(Task entry, int index) {
		assert(entry != null);		// entry has been checked and is not null

		String returnString;
		String titleDesc;
		
		String title = entry.getTitle();
		assert (title != null);		// at this point, title shouldn't be null
		
		String description = entry.getDescription();
		if (description == null) {
			description = "";
			titleDesc = title;
		} else {
			titleDesc = title + TITLE_DESC_DELIMITER + description;
		}
		
//		String location = entry.getLocation();
//		String type = entry.getType();
				
		String priority;
		if (entry.getPriority() <= Constants.LOW_PRIORITY) {
			priority = TABLE_PRIORITY_LOW;
			returnString = priority;
		} else {
			priority = TABLE_PRIORITY_HIGH;
			returnString = priority;
		}
		
		String startDate;
		if (entry.isStartDateSet()) {
			startDate = (entry.getStartDate().get(Calendar.DAY_OF_MONTH)) + DATE_DELIMITER + 
					(entry.getStartDate().get(Calendar.MONTH) + 1) + DATE_DELIMITER + 
					entry.getStartDate().get(Calendar.YEAR);
		} else {
			startDate = null;
		}
		
		String endDate;
		if (entry.isEndDateSet()) {
			endDate = (entry.getEndDate().get(Calendar.DAY_OF_MONTH)) + DATE_DELIMITER + 
					(entry.getEndDate().get(Calendar.MONTH) + 1) + DATE_DELIMITER + 
					entry.getEndDate().get(Calendar.YEAR);
		} else {
			endDate = null;
		}
		
		int hour, minute;
		String hourStr, minuteStr;
		
		String startTime;
		if (entry.isStartTimeSet()) {
			hour = entry.getStartDate().get(Calendar.HOUR_OF_DAY);
			minute = entry.getStartDate().get(Calendar.MINUTE);
			if (hour < 10) {
				hourStr = "0" + hour; 
			} else {
				hourStr = hour + "";
			}
			
			if (minute < 10) {
				minuteStr = "0" + minute; 
			} else {
				minuteStr = minute + "";
			}
			startTime = hourStr + TIME_DELIMITER + minuteStr;
		} else {
			startTime = null;
		}
		
		String endTime;
		if (entry.isEndTimeSet()) {
			hour = entry.getEndDate().get(Calendar.HOUR_OF_DAY);
			minute = entry.getEndDate().get(Calendar.MINUTE);
			if (hour < 10) {
				hourStr = "0" + hour; 
			} else {
				hourStr = hour + "";
			}
			
			if (minute < 10) {
				minuteStr = "0" + minute; 
			} else {
				minuteStr = minute + "";
			}
			endTime = hourStr + TIME_DELIMITER + minuteStr;
		} else {
			endTime = null;
		}
		
		String start;
		if (startDate == null && startTime == null) {
			start = TABLE_EMPTY_DATE_FIELD;
		} else if (startDate != null && startTime == null) {
			start = startDate;
		} else if (startDate == null && startTime != null) {
			start = startTime;
		} else {
			start = startDate + DATE_TIME_DELIMITER + startTime;
		}
		
		String end;
		if (endDate == null && endTime == null) {
			end = TABLE_EMPTY_DATE_FIELD;
		} else if (endDate != null && endTime == null) {
			end = endDate;
		} else if (endDate == null && endTime != null) {
			end = endTime;
		} else {
			end = endDate + DATE_TIME_DELIMITER + endTime;
		}
		
		if (start.length()  <= TABLE_DATE_ALLOWANCE) {
			start = String.format(TABLE_DATE_PAD, start);
		}	// assume end.length() <= TABLE_TIME_ALLOWANCE
		
		if (end.length()  <= TABLE_DATE_ALLOWANCE) {
			end = String.format(TABLE_DATE_PAD, end);
		}	// assume end.length() <= TABLE_TIME_ALLOWANCE
		
		
		// assume task is done
//		String status = TABLE_ENTRY_DONE;
		
/**
 		if (entry.isDone()) {
 			status = TABLE_ENTRY_DONE;
 		} else {
 			status = TABLE_ENTRY_UNDONE;
 		}
 */
		
/*		if (location == null) {
			location = TABLE_EMPTY_LOCATION_FIELD;
		}
		
		if (type == null) {
			type = TABLE_EMPTY_TYPE_FIELD;
		}

		if (location.length()  <= TABLE_LOCATION_ALLOWANCE) {
			location = String.format(TABLE_LOCATION_PAD, location);
		}	// assume location.length()  <= TABLE_LOCATION_ALLOWANCE. Otherwise the table will be shifted
		
		if (type.length()  <= TABLE_TYPE_ALLOWANCE) {
			type = String.format(TABLE_TYPE_PAD, type);
		}	// assume type.length() <= TABLE_TYPE_ALLOWANCE. Otherwise the table will be shifted
*/		
		if (titleDesc.length()  <= TABLE_DESCRIPTION_ALLOWANCE) {
			titleDesc = String.format(TABLE_DESCRIPTION_PAD, titleDesc);
			returnString = String.format(TABLE_ENTRY_FORMAT, index, priority, start, end, titleDesc);
		} else {
			returnString =  multiLineTitleDesc(index, priority, start, end, titleDesc);
		}
		
/*		if (location.length()  <= TABLE_LOCATION_ALLOWANCE) {
			location = String.format(TABLE_LOCATION_PAD, location);
			returnString = String.format(TABLE_ENTRY_FORMAT, index, status, priority, start, end, titleDesc, location, type);
		} 
		
		else {
			returnString =  multiLineLocation(index, status, priority, start, end, titleDesc, location, type);
		}
		
		if (type.length()  <= TABLE_TYPE_ALLOWANCE) {
			type = String.format(TABLE_TYPE_PAD, type);
			returnString = String.format(TABLE_ENTRY_FORMAT, index, status, priority, start, end, titleDesc, location, type);
		}
		
		else {
			returnString =  multiLineType(index, status, priority, start, end, titleDesc, location, type);
		}
*/
		return returnString;		
	}

	/**
	 * Display the titleDesc (title + description) field so that it fits the column width of the table allocated to it.
	 * If the length of titleDesc > space allocated to it, it is split and brought to the next line.
	 * @param index. Task index
	 * @param priority. Priority level of the task, as specified by the user.
	 * @param start. The start time and/or start date
	 * @param end. The end time and/or end date
	 * @param titleDesc. The title of task, appended by the description of task (if any)
	 * @return the task details in a table
	 */
	private String multiLineTitleDesc(int index, String priority,
			String start, String end, String titleDesc) {
		
		StringBuffer returnEntry = new StringBuffer();
		int linesRequired = (int) Math.ceil(((float) titleDesc.length()) / TABLE_DESCRIPTION_ALLOWANCE);
		
		// break description string into multiple lines to fit into the description column
		
		// The first line contains other fields and hence has to be processed differently
		String firstLine = String.format(TABLE_ENTRY_FORMAT, index, priority, start, end, titleDesc.substring(0, TABLE_DESCRIPTION_ALLOWANCE));

		returnEntry.append(firstLine);

		// Start processing from the second line
		for(int lineNumber = 2; lineNumber < linesRequired; lineNumber++ ) {
			String substringForThisLine = titleDesc.substring((lineNumber - 1) * TABLE_DESCRIPTION_ALLOWANCE, TABLE_DESCRIPTION_ALLOWANCE * lineNumber);
			String nextLine = String.format(LINE_BREAK + TABLE_ENTRY_OVERFLOW_FORMAT, substringForThisLine);
			returnEntry.append(nextLine);
		}

		// Process the last line specially as it might have to be padded with spaces
		String subStringForLastLine = titleDesc.substring((linesRequired - 1) * TABLE_DESCRIPTION_ALLOWANCE);
		String lastLineDescription = String.format(TABLE_DESCRIPTION_PAD, subStringForLastLine);
		String lastLine = String.format(LINE_BREAK + TABLE_ENTRY_OVERFLOW_FORMAT, lastLineDescription);
		returnEntry.append(lastLine);

		return returnEntry.toString();
	}

	@Override
	public void run() {
		runUI();
		
	}

/*	
	private String multiLineLocation(int index, String status, String priority,
			String start, String end, String titleDesc, String location,
			String type) {
		
		StringBuffer returnEntry = new StringBuffer();
		int linesRequired = (int) Math.ceil(((float) location.length()) / TABLE_LOCATION_ALLOWANCE);
		
		// break location string into multiple lines to fit into the location column
		
		// The first line contains other fields and hence has to be processed differently
		String firstLine = String.format(TABLE_ENTRY_FORMAT, index, status, priority, start, end, titleDesc, location.substring(0, TABLE_LOCATION_ALLOWANCE), type);

		returnEntry.append(firstLine);

		// Start processing from the second line
		for(int lineNumber = 2; lineNumber < linesRequired; lineNumber++ ) {
			String substringForThisLine = location.substring((lineNumber - 1) * TABLE_LOCATION_ALLOWANCE, TABLE_LOCATION_ALLOWANCE * lineNumber);
			String nextLine = String.format(LINE_BREAK + TABLE_ENTRY_OVERFLOW_FORMAT, substringForThisLine);
			returnEntry.append(nextLine);
		}

		// Process the last line specially as it might have to be padded with spaces
		String subStringForLastLine = location.substring((linesRequired - 1) * TABLE_LOCATION_ALLOWANCE);
		String lastLineLocation = String.format(TABLE_LOCATION_PAD, subStringForLastLine);
		String lastLine = String.format(LINE_BREAK + TABLE_ENTRY_OVERFLOW_FORMAT, lastLineLocation);
		returnEntry.append(lastLine);

		return returnEntry.toString();
	}
	
	private String multiLineType(int index, String status, String priority,
			String start, String end, String titleDesc, String location,
			String type) {
		
		StringBuffer returnEntry = new StringBuffer();
		int linesRequired = (int) Math.ceil(((float) type.length()) / TABLE_TYPE_ALLOWANCE);
		
		// break type string into multiple lines to fit into the type column
		
		// The first line contains other fields and hence has to be processed differently
		String firstLine = String.format(TABLE_ENTRY_FORMAT, index, status, priority, start, end, titleDesc, location, type.substring(0, TABLE_TYPE_ALLOWANCE));

		returnEntry.append(firstLine);

		// Start processing from the second line
		for(int lineNumber = 2; lineNumber < linesRequired; lineNumber++ ) {
			String substringForThisLine = type.substring((lineNumber - 1) * TABLE_TYPE_ALLOWANCE, TABLE_TYPE_ALLOWANCE * lineNumber);
			String nextLine = String.format(LINE_BREAK + TABLE_ENTRY_OVERFLOW_FORMAT, substringForThisLine);
			returnEntry.append(nextLine);
		}

		// Process the last line specially as it might have to be padded with spaces
		String subStringForLastLine = type.substring((linesRequired - 1) * TABLE_TYPE_ALLOWANCE);
		String lastLineType = String.format(TABLE_TYPE_PAD, subStringForLastLine);
		String lastLine = String.format(LINE_BREAK + TABLE_ENTRY_OVERFLOW_FORMAT, lastLineType);
		returnEntry.append(lastLine);

		return returnEntry.toString();
	}
*/
}